package com.wantup.springboot.boot.contorller;

import com.wantup.springboot.boot.beans.User;
import com.wantup.springboot.boot.service.LoginServicr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2018/11/13 18:45
 */
@Controller
public class LoginController {

    @Autowired
    private LoginServicr loginServicr;


    @PostMapping("login")
    public String userLogin(String username, String password, HttpServletRequest request, HttpSession session) throws Exception {
        String trans = "";
        try{
            Map<String,String> stringMap = new HashMap<>();
            stringMap.put("username",username);
            stringMap.put("password",password);
            User user =  this.loginServicr.login(stringMap);
                System.out.println("传入参数 ："+username +"   "+password );
            if (user != null){
                //该用户存在
                session.setAttribute("user",user);

                System.out.println("该用户 ---> 存在");
                trans = "redirect:main";
            }else{
                //用户不存在
                System.out.println("该用户 ---> 不存在");
                trans = "redirect:error";
            }

        }catch (Exception e){
            e.printStackTrace();
            //程序执行异常
            trans = "redirect:error";
        }



        return trans;
    }



}
